/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgamesgui;

/**
 *
 * @author Fabio Mendonca
 */
import java.awt.Color;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import javax.print.attribute.standard.Sides;
import olympicgameslogic.Edition;
import olympicgameslogic.LinkedList;
import olympicgameslogic.Nation;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Year;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;


public class MultiLineChart extends ApplicationFrame {
   
   @Override
   public void windowClosing(final WindowEvent evt){
      if(evt.getWindow() == this){
	 dispose();
      }
   }

   private LinkedList<Nation> nations;
   private XYSeries[] series;
   private Nation[] nationsArray;

   public MultiLineChart(final String title, LinkedList<Nation> nts) {
      super(title);
      series = new XYSeries[nts.size()];
      nationsArray = new Nation[nts.size()];
      for (int i = 0; i < nationsArray.length; i++) {
	 nationsArray[i] = nts.getElement(i);
      }
      final XYDataset dataset = createDataset();
      final JFreeChart chart = createChart(dataset);
      final ChartPanel chartPanel = new ChartPanel(chart);
      chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
      setContentPane(chartPanel);
      pack();
      setVisible(true);
   }

   private XYDataset createDataset() {

      for (int i = 0; i < series.length; i++) {
	 series[i] = new XYSeries(nationsArray[i].getCode());
	 for (Edition ed : OlympicGamesGui.ap.getEditions()) {
	    if (ed.existsNation(nationsArray[i])) {
	       series[i].add((int)(ed.getYear()), (int)nationsArray[i].getTotalEditionMedals(ed));
	    }else
	       series[i].add((int)ed.getYear(), (int)0);
	 }
      }
      final XYSeriesCollection dataset = new XYSeriesCollection();
      for (int i = 0; i < series.length; i++) {
	 dataset.addSeries(series[i]);
      }
      return dataset;
   }


   
   private JFreeChart createChart(final XYDataset dataset) {
      
      //DateAxis axis = (DateAxis
      
      final JFreeChart chart = ChartFactory.createXYLineChart(
	      "Medals Comparisson",
	      "Editions",
	      "Medals",
	      dataset,
	      PlotOrientation.VERTICAL,
	      true,
	      true,
	      false);
      chart.setBackgroundPaint(Color.white);

      final XYPlot plot1 = chart.getXYPlot();
      plot1.setBackgroundPaint(Color.lightGray);
      plot1.setDomainGridlinePaint(Color.white);
      plot1.setRangeGridlinePaint(Color.white);
      
      final XYPlot plot2 = chart.getXYPlot();
      plot2.setBackgroundPaint(Color.lightGray);
      plot2.setDomainGridlinePaint(Color.white);
      plot2.setRangeGridlinePaint(Color.white);

      final XYPlot plot3 = chart.getXYPlot();
      plot3.setBackgroundPaint(Color.lightGray);
      plot3.setDomainGridlinePaint(Color.white);
      plot3.setRangeGridlinePaint(Color.white);

      final XYPlot plot4 = chart.getXYPlot();
      plot4.setBackgroundPaint(Color.lightGray);
      plot4.setDomainGridlinePaint(Color.white);
      plot4.setRangeGridlinePaint(Color.white);

      final XYPlot plot5 = chart.getXYPlot();
      plot5.setBackgroundPaint(Color.lightGray);
      plot5.setDomainGridlinePaint(Color.white);
      plot5.setRangeGridlinePaint(Color.white);

      return chart;
   }
   
   
   
}